package com.zdy.im.conf;

import com.sixeco.framework.http.inters.IProjectModule;
import com.sixeco.framework.http.inters.ProjectModuleParameter;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author 作者 : zdy
 * @CreateDate 创建时间: 2021-02-30
 * @WeeK 星期: 星期三
 * @Version 版本: v1.0.0
 * <pre>
 *
 *  @Description (说明):  im服务端口管理类
 *
 * </pre>
 **/

public enum  ImModuleEnum implements IProjectModule, ProjectModuleParameter {

    ALL(0,MODULE_ALL,MODULE_ALL, SERVER_HTTP_TYPE),

    HTTP_DEFAULT(PORT_HTTP_DEFAULT,MODULE_DEFAULT,MODULE_DEFAULT, SERVER_HTTP_TYPE),
    IM_HTTP(19010,"im-http","im-http", SERVER_HTTP_TYPE),
    IM_SOCKET( 19020 ,"im-socket" ,"im-socket" , SERVER_RPC_TYPE),
;
    private int port;
    private String module;
    private String serverName;
    private int serverType;

    private static Map<String, ImModuleEnum> enumMap = new HashMap<>();

    static {

        for (ImModuleEnum type : values()) {
            enumMap.put(type.serverName, type);
        }
    }

    ImModuleEnum(int port, String module, String serverName, int serverType) {
        this.port = port;
        this.module = module;
        this.serverName = serverName;
        this.serverType = serverType;
    }

    @Override
    public boolean isRpc() {
        return this.serverType == SERVER_RPC_TYPE ? true : false;
    }

    public static IProjectModule getModuleServerNameEnum(String serverName) {
        ImModuleEnum moduleEnum = enumMap.get(serverName);
        moduleEnum = moduleEnum == null ? HTTP_DEFAULT : moduleEnum;
        return moduleEnum;
    }

    @Override
    public String getModule() {
        return module;
    }

    @Override
    public int getPort() {
        return port;
    }

    public String getServerName() {
        return serverName;
    }

    @Override
    public String getConfigModulePort() {
        String configModule = this.module;
        StringBuilder sb = new StringBuilder();
        if (isRpc()) {
            sb.append(CONFIG_RPC);
        } else {
            sb.append(CONFIG_HTTP);
        }
        return sb.append(configModule).toString();
    }

    @Override
    public String getRunModuleServerName() {
        StringBuilder sb = new StringBuilder();
        if (isRpc()) {
            sb.append(SERVER_RPC);
        } else {
            sb.append(SERVER_HTTP);
        }
        return sb.append(module).toString();
    }

}



